package com.coolgor.coldot.dao.auth;

import java.util.List;

import com.coolgor.coldot.entity.auth.LoginInfo;
import org.apache.ibatis.annotations.*;


@Mapper
public interface LoginInfoDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_log
			<where>
				<if test="loginInfoCondition.recordID != null">
					and record_id = #{loginInfoCondition.recordID}
				</if>			
				<if test="loginInfoCondition.userID != null">
					and user_id = #{loginInfoCondition.userID}
				</if>
				<if test="loginInfoCondition.username != null">
					and username like '%${loginInfoCondition.username}%'
				</if>
				<if test="loginInfoCondition.nameCN != null">
					and name_cn like '%${loginInfoCondition.nameCN}%'
				</if>		
				<if test="loginInfoCondition.loginRoleId != null">
					and login_role_id = #{loginInfoCondition.loginRoleId}
				</if>
				<if test="loginInfoCondition.loginRoleName != null">
					and login_role_name = #{loginInfoCondition.loginRoleName}
				</if>
				<if test="loginInfoCondition.loginIP != null">
					and login_ip = #{loginInfoCondition.loginIP}
				</if>
				<if test="loginInfoCondition.loginTime != null">
					and login_time = #{loginInfoCondition.loginTime}
				</if>
				<if test="loginInfoCondition.loginWay != null">
					and login_way = #{loginInfoCondition.loginWay}
				</if>
				<if test="loginInfoCondition.status != null">
					and status = #{loginInfoCondition.status}
				</if>
				<if test="loginInfoCondition.statusInfo != null">
					and status_info = #{loginInfoCondition.statusInfo}
				</if>
				<if test="loginInfoCondition.remark != null">
					and remark like '%${loginInfoCondition.remark}%'
				</if>	
			</where>
			ORDER BY login_time DESC
			LIMIT #{rowIndex},#{pageSize};					
		</script>
	""")
	@Results(id = "LoginInfoMap", value = {
			@Result(property = "recordID", column = "record_id"),
			@Result(property = "userID", column = "user_id"),
			@Result(property = "username", column = "username"),
			@Result(property = "nameCN", column = "name_cn"),
			@Result(property = "loginRoleId", column = "login_role_id"),
			@Result(property = "loginRoleName", column = "login_role_name"),
			@Result(property = "loginIP", column = "login_ip"),
			@Result(property = "loginTime", column = "login_time"),
			@Result(property = "loginWay", column = "login_way"),
			@Result(property = "status", column = "status"),
			@Result(property = "statusInfo", column = "status_info"),
			@Result(property = "remark", column = "remark")
	})
	List<LoginInfo> queryLoginInfoList(@Param("loginInfoCondition") LoginInfo loginInfoCondition,
									   @Param("rowIndex") int rowIndex,
									   @Param("pageSize") int pageSize);


	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_auth_log
			<where>
				<if test="loginInfoCondition.recordID != null">
					and record_id = #{loginInfoCondition.recordID}
				</if>			
				<if test="loginInfoCondition.userID != null">
					and user_id = #{loginInfoCondition.userID}
				</if>
				<if test="loginInfoCondition.username != null">
					and username like '%${loginInfoCondition.username}%'
				</if>
				<if test="loginInfoCondition.nameCN != null">
					and name_cn like '%${loginInfoCondition.nameCN}%'
				</if>		
				<if test="loginInfoCondition.loginRoleId != null">
					and login_role_id = #{loginInfoCondition.loginRoleId}
				</if>
				<if test="loginInfoCondition.loginRoleName != null">
					and login_role_name = #{loginInfoCondition.loginRoleName}
				</if>
				<if test="loginInfoCondition.loginIP != null">
					and login_ip = #{loginInfoCondition.loginIP}
				</if>
				<if test="loginInfoCondition.loginTime != null">
					and login_time = #{loginInfoCondition.loginTime}
				</if>
				<if test="loginInfoCondition.loginWay != null">
					and login_way = #{loginInfoCondition.loginWay}
				</if>
				<if test="loginInfoCondition.status != null">
					and status = #{loginInfoCondition.status}
				</if>
				<if test="loginInfoCondition.statusInfo != null">
					and status_info = #{loginInfoCondition.statusInfo}
				</if>
				<if test="loginInfoCondition.remark != null">
					and remark like '%${loginInfoCondition.remark}%'
				</if>	
			</where>					
		</script>
	""")
	int queryLoginInfoCount(@Param("loginInfoCondition") LoginInfo loginInfoCondition);


	@Insert("""
		<script>
			INSERT INTO tb_coldot_auth_log
				(user_id,username,name_cn,login_role_id,login_role_name,login_ip,login_time,
				login_way,status,status_info,remark)
			VALUES
				(#{userID},#{username},#{nameCN},#{loginRoleId},#{loginRoleName},#{loginIP},
				#{loginTime},#{loginWay},#{status},#{statusInfo},#{remark})					
		</script>
	""")
	@Options(useGeneratedKeys = true, keyColumn = "record_id", keyProperty = "recordID")
	int insertLoginInfo(LoginInfo loginInfo);


	@Update("""
		<script>
			UPDATE
				tb_coldot_auth_log
			<set>
				<if test="userID != null">user_id=#{userID},</if>
				<if test="username != null">username=#{username},</if>
				<if test="nameCN != null">name_cn=#{nameCN},</if>
				<if test="loginRoleId != null">login_role_id=#{loginRoleId},</if>
				<if test="loginRoleName != null">login_role_name=#{loginRoleName},</if>
				<if test="loginIP != null">login_ip=#{loginIP},</if>
				<if test="loginTime != null">login_time=#{loginTime},</if>
				<if test="loginWay != null">login_way=#{loginWay},</if>
				<if test="status != null">status=#{status},</if>
				<if test="statusInfo != null">status_info=#{statusInfo}</if>
				<if test="remark != null">remark=#{remark}</if>
			</set>
			WHERE
				record_id = #{recordID}					
		</script>
	""")
	int updateLoginInfo(LoginInfo loginInfo);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_log
			WHERE
				record_id = #{recordID}					
		</script>
	""")
	int deleteLoginInfo(LoginInfo loginInfo);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_log
			WHERE record_id IN
			<foreach collection="list" item="recordID" open="(" separator="," close=")">
				#{recordID}
			</foreach>					
		</script>
	""")
	int batchDeleteLoginInfo(List<Long> recordIDList);

}
